#import "DDPersonViewController.h"
#import "DDCouponViewController.h"
#import "DDBindViewController.h"
#import "DDPersonCell.h"
#import "DDWebView.h"
#import "DDZFModel.h"
@interface DDPersonViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *talbeView;
@property (nonatomic, strong) DDButton *serviceBtn;
@property (nonatomic, strong) DDWebView *webView;
@property (nonatomic, strong) DDZFModel *model;
@property (nonatomic, strong) NSArray *array;
@end
@implementation DDPersonViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = @[@"用户ID",@"钱包余额",@"优惠券",@"兑换码",@"修改密码",@"绑定手机"];
}
- (UITableView *)talbeView
{
    if (!_talbeView)
    {
        _talbeView = [[UITableView alloc] initWithFrame:CGRectZero];
        _talbeView.backgroundColor = [UIColor clearColor];
        _talbeView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _talbeView.bounces = NO;
        _talbeView.delegate = self;
        _talbeView.dataSource = self;
        _talbeView.hidden = YES;
        [self.ddview addSubview:_talbeView];
        _talbeView.c_top.c_equal(self.ddview.bar.c_bottom).with.coffset(GAP);
        _talbeView.c_left.c_equal(self.ddview).with.coffset(GAP);
        _talbeView.c_right.c_equal(self.ddview).with.coffset(-GAP);
        _talbeView.c_bottom.c_equal(self.serviceBtn.c_top).with.coffset(-GAP);
    }
    return _talbeView;
}
- (DDButton *)serviceBtn
{
    if (!_serviceBtn)
    {
        _serviceBtn = [[DDButton alloc] init];
        _serviceBtn.title = @"如有疑问，请联系客服QQ";
        _serviceBtn.font = FONT(UIFontWeightRegular, 15);
        [_serviceBtn setValue:[DDHelper help].color forKeyPath:@"_label.textColor"];
        _serviceBtn.autoTitleSize = YES;
        [_serviceBtn addTarget:self action:@selector(dd_actionForServiceBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.ddview addSubview:_serviceBtn];
        _serviceBtn.c_bottom.c_equal(self.ddview).with.coffset(-GAP);
        _serviceBtn.c_centerX.c_equal(self.ddview);
    }
    return _serviceBtn;
}
- (DDWebView *)webView
{
    if (!_webView)
    {
        _webView = [[DDWebView alloc] init];
        [self.ddview addSubview:_webView];
    }
    return _webView;
}
- (void)dd_actionForServiceBtn
{
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]])
    {
        [self.webView dd_show:[NSString stringWithFormat:@"mqq://im/chat?chat_type=wpa&uin=%@&version=1&src_type=web",[DDHelper help].qq]];
    }
    else
    {
        [CCAlert showMessage:@"未安装QQ,请先安装" title:@"注意" enter:nil];
    }
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"DDPersonCell";
    DDPersonCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell = [[DDPersonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.title = self.array[indexPath.row];
    if (indexPath.row == 0)
    {
        cell.content = [DDHelper help].accountID;
    }
    else if (indexPath.row == 1)
    {
        cell.content = [NSString stringWithFormat:@"%@元",self.model.balance];
    }
    else if (indexPath.row == 2)
    {
        cell.content = [NSString stringWithFormat:@"%lu张可用>>",(unsigned long)[self.model.coupons count]];
        [cell setValue:[DDHelper help].color forKeyPath:@"_contentLBL.textColor"];
    }
    else if (indexPath.row == 3)
        cell.content = @"去兑换>>";
    else if (indexPath.row == 4)
        cell.content = @"去修改>>";
    else if (indexPath.row == 5)
        cell.content = [[DDHelper help].phone length] > 0?@"已经绑定":@"去绑定>>";
    else
        cell.content = [NSString stringWithFormat:@"99%d",indexPath.row+1];
    return cell;
}
- (void)dd_screenRotate:(UIInterfaceOrientation)orientation
{
    [super dd_screenRotate:orientation];
}
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return JTWPERSONCELLHEIGHT;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 2 && self.model.coupons)
    {
        DDCouponViewController *vc = [[DDCouponViewController alloc] init];
        vc.coupons = self.model.coupons;
        vc.discoupons = self.model.discoupons;
        vc.selectIndex = self.model.coupons.count + 100;
//                vc.selectIndex = self.model.coupons.count;
        vc.showUseBtn = NO;
        [self presentViewController:vc animated:YES completion:nil];
    }
    else if (indexPath.row == 3)
        [self presentViewController:[NSClassFromString(@"DDExchangeViewController") new] animated:YES completion:nil];
    else if (indexPath.row == 4)
        [self presentViewController:[NSClassFromString(@"DDPassViewController") new] animated:YES completion:nil];
    else if (indexPath.row == 5 && [[DDHelper help].phone length] < 1)
        [self presentViewController:[NSClassFromString(@"DDBindViewController") new] animated:YES completion:nil];
}
#pragma mark - JTWRequestDelegate
- (void)dd_didRequestWithReceiveData:(NSDictionary *)dict error:(NSError *)error
{
    [CCHud stopHud];
    if(error)
    {
        if ((error.code != -999 || ![error.localizedDescription isEqualToString:@"cancelled"]))
            [CCAlert showMessage:[NSString stringWithFormat:@"%@",error.localizedDescription] hidden:nil];
        return;
    }
    if (!dict)
        return;
    int code = -1;
    code = [[dict valueForKey:@"code"] intValue];
    NSString *info = [NSString stringWithFormat:@"%@",[dict valueForKey:@"info"]];
    if (code == 0)
    {
        self.model = [DDZFModel modelWithDict:dict];
        [self.talbeView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0],[NSIndexPath indexPathForRow:2 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
    }
    else
    {
        [CCAlert showMessage:[NSString stringWithFormat:@"%@" ,info] title:@"错误" enter:nil];
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self reload];
}
- (void)reload
{
    self.ddview.bar.title = @"个人信息";
    self.serviceBtn.hidden = NO;
    self.talbeView.hidden = NO;
    [self.talbeView reloadData];
    [CCHud startHud];
    [self.request dd_prepareZFRequest:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
